package com.itcast.yygh.user.controller;

import com.itcast.common.result.R;
import com.itcast.common.utils.AuthContextHolder;
import com.itcast.yygh.model.model.user.Patient;
import com.itcast.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 就诊人管理接口
 */
@Api(tags = "就诊人管理接口")
@Slf4j
@RestController
@RequestMapping("/api/userinfo/patient")
@RequiredArgsConstructor
public class PatientApiController {
    private final PatientService patientService;


    //获取就诊人列表
    @ApiOperation("根据登录的用户id查询所有就诊人信息")
    @GetMapping("auth/findAll")
    public R findAll(HttpServletRequest request) {
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllUserId(userId);
        return R.ok().data("list",list);
    }
    //保存就诊人
    @ApiOperation("保存就诊人信息")
    @PostMapping("auth/save")
    public R savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        try{
            patientService.save(patient);
            return R.ok();
        }catch (Exception e) {
            log.error(e.toString());
            return R.error().message(e.toString());
        }
    }
    //根据id查询单个就诊人信息
    @ApiOperation("根据id查询单个就诊人信息")
    @GetMapping("auth/get/{id}")
    public R getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientId(id);
        return R.ok().data("patient",patient);
    }
    //修改就诊人信息
    @ApiOperation("修改就诊人信息")
    @PostMapping("auth/update")
    public R updatePatient(@RequestBody Patient patient) {
        try {
            patientService.updateById(patient);
            return R.ok();
        }catch (Exception e) {
            log.error(e.toString());
            return R.error().message(e.toString());
        }
    }
    //删除就诊人
    @ApiOperation("根据id删除就诊人")
    @DeleteMapping("auth/remove/{id}")
    public R removePatient(@PathVariable Long id) {
        try {
            patientService.removeById(id);
            return R.ok();
        }catch (Exception e) {
            log.error(e.toString());
            return R.error().message(e.toString());
        }
    }

    @ApiOperation("获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(
            @ApiParam(name = "id", value = "就诊人id", required = true)
            @PathVariable("id") Long id) {
        return patientService.getById(id);
    }
}